package com.baofu.trade.service.impl;

import com.baofu.trade.model.TakerLongShortRatio;
import com.baofu.trade.service.BinanceApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.List;

@Slf4j
@Service
public class BinanceApiServiceImpl implements BinanceApiService {
    
    private final RestTemplate restTemplate;
    private final String BINANCE_FUTURES_API_URL = "https://fapi.binance.com";
    
    public BinanceApiServiceImpl() {
        this.restTemplate = new RestTemplate();
    }
    
    @Override
    public List<TakerLongShortRatio> getTakerLongShortRatio(
            String symbol, 
            String period, 
            Integer limit,
            Long startTime,
            Long endTime) {
        try {
            StringBuilder urlBuilder = new StringBuilder(
                String.format("%s/futures/data/takerlongshortRatio?symbol=%s&period=%s",
                    BINANCE_FUTURES_API_URL, symbol, period));
            
            if (limit != null) {
                urlBuilder.append("&limit=").append(limit);
            }
            if (startTime != null) {
                urlBuilder.append("&startTime=").append(startTime);
            }
            if (endTime != null) {
                urlBuilder.append("&endTime=").append(endTime);
            }
            
            String url = urlBuilder.toString();
            log.debug("请求URL: {}", url);
            
            return restTemplate.getForObject(url, List.class);
            
        } catch (Exception e) {
            log.error("获取主动买卖方向比率失败: symbol={}, period={}", symbol, period, e);
            throw new RuntimeException("获取数据失败: " + e.getMessage());
        }
    }
} 